/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> stk;
    vector<vector<int>> ans;
    bool isLeaf(TreeNode* root)
    {
        if(root==nullptr)
            return false;
        return(!root->left && !root->right);
    }
    void dfs(TreeNode* root,int target)
    {
        if(root==nullptr)
            return;
        if(isLeaf(root))
        {
            if(target==0)
            {
                ans.push_back(stk);
            }
            return;
        }
        if(root->left)
        {
            stk.push_back(root->left->val);
            dfs(root->left,target-root->left->val);
            stk.pop_back();
        }
        if(root->right)
        {
            stk.push_back(root->right->val);
            dfs(root->right,target-root->right->val);
            stk.pop_back();
        }
    }
    vector<vector<int>> pathSum(TreeNode* root, int target) {
        if(root==nullptr)
            return ans;
        stk.push_back(root->val);
        dfs(root,target-root->val);
        return ans;
    }
};